package com.umeox.watch.motioncapture.model

class DataPackage {
    private var dataList = arrayListOf<GsensorData>()
    fun fillInData(data: GsensorData) {
        dataList.add(data)
    }

    fun checkFill(): Boolean {
        return dataList.size == 50
    }

    fun getData(): String {
        return listOf(
            getTimestamp(),
            getAccX(),
            getAccY(),
            getAccZ()
        ).joinToString(",")
    }

    private fun getTimestamp(): String = "${dataList.first().timestamp}"

    private fun getAccX(): String {
        val list = dataList.map { it.x }
        val sb = StringBuilder("\"")
        for ((index, i) in list.withIndex()) {
            sb.append("$i")
            if (index != list.size - 1) {
                sb.append(", ")
            }
        }
        sb.append("\"")
        return sb.toString()
    }

    private fun getAccY(): String {
        val list = dataList.map { it.y }
        val sb = StringBuilder("\"")
        for ((index, i) in list.withIndex()) {
            sb.append("$i")
            if (index != list.size - 1) {
                sb.append(", ")
            }
        }
        sb.append("\"")
        return sb.toString()
    }

    private fun getAccZ(): String {
        val list = dataList.map { it.z }
        val sb = StringBuilder("\"")
        for ((index, i) in list.withIndex()) {
            sb.append("$i")
            if (index != list.size - 1) {
                sb.append(", ")
            }
        }
        sb.append("\"")
        return sb.toString()
    }
}